<?php

namespace app\serversetting\traits;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-08-17 09:48:23
 * @Description: 此方法操作的表的特点：（操作setting表）
 * 1、传入app_id进一步验证。 
 * 2、拥有分类,左连接分类，靠分类中的app_id、other_app_read、is_public来进行区分应用
 */

trait OperateModel
{

	/*当作模板来看*/
	/*public function getOneSet($data): array 
	{
		isset($data['id']) && $_where['s.id'] = $data['id'];
        isset($data['action']) && $_where['s.action'] = $data['action'];
        $_where['s.status'] = ['neq',-1];
        $_where['sc.status'] = ['neq',-1];
        $isManageAppId = $this->_isManageAppId($data['app_id']);
        $_whereOr = $isManageAppId ? true : "`sc`.`app_id` = {$data['app_id']} OR FIND_IN_SET({$data['app_id']},`other_app_read`) OR sc.is_public = 1";

    	$res = $this->model($this->settingModel)
                    ->alias('s')
                    ->join('setting_category sc', 'sc.id = s.category_id', 'LEFT')
                    ->where($_where)
                    ->where($_whereOr)
                    ->find();

        $res && $res = $res->toArray();
    	return auto_return($res);
	}*/
	/*编辑一个正常的<status = 1>记录（例如：编辑一条配置）*/
	private function _editOne(array $data, string $editTableName, string $joinCateTableName, $Db): array 
    {
        $_where[$editTableName . '.id'] = $data['id'];
        $_where[$editTableName . '.status'] = 1;
        //$_where[$joinCateTableName . '.status'] = ['neq',-1];

        $manageAppId = ApiBase::getAppId();
        $manageAppId != $data['app_id'] && $_where[$joinCateTableName . '.app_id'] = $data['app_id'];

        $data = add_data_table_name($data, $editTableName, 'id,app_id');
        foreach ($data as $k => &$v) {
            is_array($v) && $v = json_encode($v);
        }
        
    	$res = $Db->alias($editTableName)
                ->join($joinCateTableName, $joinCateTableName . '.id = ' . $editTableName . '.category_id', 'LEFT')
                ->where($_where)
                ->update($data);
    	return auto_return($res);
    }

    /*后台获取某条不被删除的记录 （例如：获取某条配置详情）*/
    private function _getOne(array $data, string $editTableName, string $joinCateTableName, $Model): array 
    {
    	isset($data['id']) && $_where[$editTableName . '.id'] = $data['id'];
        isset($data['action']) && $_where[$editTableName . '.action'] = $data['action'];
        $_where[$editTableName . '.status'] = ['neq',-1];
        //$_where[$joinCateTableName . '.status'] = ['neq',-1];

        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : $joinCateTableName . '.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR ' . $joinCateTableName . '.is_public = 1';

    	$res = $Model->alias($editTableName)
                ->field($editTableName . '.*,app_id,other_app_read,is_public')
                ->join($joinCateTableName, $joinCateTableName . '.id = ' . $editTableName . '.category_id', 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->find();

        $res && $res = $res->toArray();
    	return auto_return($res);
    }

    /*后台获取某应用下的不被删除的分页列表记录*/
    private function _getList(array $data, string $editTableName, string $joinCateTableName, $Model, bool $isPageList = true, string $cateIdFieldName = 'category_id'): array 
    {
    	$_where[$editTableName . '.status'] = ['neq',-1];
        //$_where[$joinCateTableName . '.status'] = ['neq',-1];
        
        isset($data['user_id']) && $_where[$editTableName . '.user_id'] = $data['user_id'];

        if(isset($data['category_action'])){   //分类表中的action
        	is_string($data['category_action']) && $data['category_action'] = explode(",", $data['category_action']);
        	$_where[$joinCateTableName . '.action'] = ['in',$data['category_action']];
        }
        if(isset($data['action'])){   //非分类表中的action
        	is_string($data['action']) && $data['action'] = explode(",", $data['action']);
        	$_where[$editTableName . '.action'] = ['in',$data['action']];
        }

        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : $joinCateTableName . '.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR ' . $joinCateTableName . '.is_public = 1';

        if($isPageList){   //返回分页列表数据
        	$res = $Model->alias($editTableName)
                ->field($editTableName . '.*,app_id,other_app_read,is_public')
                ->join($joinCateTableName, $joinCateTableName . '.id = ' . $editTableName . '.' . $cateIdFieldName, 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->order($editTableName . '.create_time', 'desc')
                ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                ->toArray();

        	return auto_return_page($res);
        }else{
        	$res = $Model->alias($editTableName)
                ->field($editTableName . '.*,app_id,other_app_read,is_public')
                ->join($joinCateTableName, $joinCateTableName . '.id = ' . $editTableName . '.' . $cateIdFieldName, 'LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->order($editTableName . '.create_time', 'desc')
                ->select();
                
        	return auto_return($res);
        }
        
    }
    



}